﻿using System.Data;
using System.Collections.Generic;
using Model.EasyUI;
using SqlHelper;

namespace Service.Content
{
    public class CategoryService : Repository<Model.Content.Category>
    {
        /// <summary>
        /// 获取操作相关DataTable
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="ParentID">父级ID</param>
        /// <returns></returns>
        public DataTable GetCategoryDataTable(int UserID, int ParentID)
        {
            string sql = $"SELECT CategoryID,CategoryName,ParentID FROM	TCategory WHERE	UserID ={ UserID } AND  ParentID ={ ParentID } AND IsDelete=0";
            return GetDataTableSQL(sql);
        }

        /// <summary>
        /// 获取树形菜单 
        /// </summary>
        /// <param name="UserID"></param> 
        /// <param name="ParentID"></param> 
        public List<TreeBox> GetTreeBoxList(int UserID, int ParentID = 0)
        {
            List<TreeBox> list = new List<TreeBox>();
            var actiondt = GetCategoryDataTable(UserID, ParentID);
            if (actiondt != null && actiondt.Rows.Count > 0)
            {
                foreach (DataRow item in actiondt.Rows)
                {
                    TreeBox tree = new TreeBox()
                    {
                        id = (int)item["CategoryID"],
                        text = item["CategoryName"].ToString(),
                        children = GetTreeBoxList(UserID, (int)item["CategoryID"])
                    };
                    list.Add(tree);
                }
            }
            else
                list = null;
            return list;
        } 
    }
}
